<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>计算属性</title>
    <style>

    </style>
</head>

<body>
    <div id="app">
        <h2>{{firstName+' '+lastName}}</h2>
        <h2>{{firstName}} {{lastName}}</h2>
        <h2>{{getFullNAME()}}</h2>
        <h2>{{fullName}}</h2>
        <h2>{{fullName2}}</h2>
        <h2>总价格：{{countPrice}}</h2>
    </div>
    <script src="./vue.js"></script>
    <script>
        let app = new Vue({
            el: "#app",
            data: {
                firstName:"kobe",
                lastName:"brant",
                books:[
                    {id:0,name:'Java编程',price:200},
                    {id:3,name:'Unix编程艺术',price:30},
                    {id:6,name:'深入理解计算机原理',price:50},
                    {id:7,name:'现代操作系统',price:100},
                ]

            },
            methods: {
                getFullNAME(){
                    return this.firstName +' '+this.lastName
                }
            },
            //计算属性
            computed: {
                // 计算属性简写，只有get方法
               fullName(){
                   return this.firstName +' '+this.lastName
               },

            //    计算属性完整写法
               fullName2:{
                    get(){
                        return this.firstName +' '+this.lastName
                    },
                    // set方法一般不用，只读属性,在重新设置值的时候会被调用，新值为newValue
                    set(newValue){
                        console.log('set'+newValue);
                        const names=newValue.split(' ')
                        this.firstName=names[0]
                        this.lastName=names[1]
                    }
               },
               countPrice(){
                    return this.books.reduce((total, currentValue, currentIndex, arr)=>{
                        return total+currentValue.price
                    },0)

                    // es6新的for循环语法
                    // for(let i in this.books){
                    //     this.books[i]
                    // }
                    // for(let book of this.books){
                    //     book
                    // }
               } 
            },
        })

    </script>
</body>

</html>